/**
 * Copyright 2010 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package saig.engine.editors;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import saig.engine.models.Tile;
import saig.engine.resources.TileLoader;

/**
 * Tile selector pane for the Map Editor.
 * @author Adam
 * @version $Id$
 * 
 * Created on Aug 15, 2010 at 2:45:03 PM 
 */
public class TilePane extends JPanel {
	/**
	 * Private TileButton class used to display and allow user interaction
	 * with a tile in the Tile Pane.
	 */
	private class TileButton extends JToggleButton implements ActionListener {
		/**
		 * Serial Version UID.
		 */
		private static final long serialVersionUID = 3725297641758170112L;
		/**
		 * The tile for this tile button.
		 */
		private Tile tile = null;
		/**
		 * Constructs a tile button.
		 * @param tile The tile to set this button to.
		 */
		public TileButton(final Tile tile) {
			super();
			this.tile = tile;
			Image tileImage = this.tile.getImage();
			setIcon(new ImageIcon(tileImage));
			setMargin(new Insets(2,2,2,2));
			addActionListener(this);
		}
		/**
		 * {@inheritDoc}
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(final ActionEvent e) {
			target = tile;
			tileLabel.setText(target.getType().name());
		}
	}
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = -6115301295670861469L;
	/**
	 * The target tile (currently selected).
	 */
	private Tile target = null;
	/**
	 * The tile button panel.
	 */
	private JPanel tileButtonPanel = null;
	/**
	 * The label for the currently selected tile.
	 */
	private JLabel tileLabel = null;
	/**
	 * The list of tiles.
	 */
	private List<Tile> tiles = null;
	/**
	 * Constructs the tile pane with a map of tiles.
	 * @param tileMap The map of tiles.
	 */
	public TilePane() {
		this.tiles = TileLoader.getInstance().getTiles();
		setLayout(new BorderLayout());
		add(getTileButtonPanel(), BorderLayout.NORTH);
		add(getTileLabel(), BorderLayout.SOUTH);
	}
	/**
	 * Returns target.
	 * @return the target.
	 */
	public Tile getTarget() {
		return target;
	}
	/**
	 * Returns the tile button panel.
	 * @return The tile button panel.
	 */
	private JPanel getTileButtonPanel() {
		if (tileButtonPanel == null) {
			tileButtonPanel = new JPanel(new GridLayout(0, 3, 2, 2));
			final ButtonGroup group = new ButtonGroup();
			for (Tile tile : tiles) {
				final TileButton tileButton = new TileButton(tile);
				group.add(tileButton);
				tileButtonPanel.add(tileButton);
				if (target == null) {
					target = tile;
					tileButton.setSelected(true);
					if (tileLabel != null) {
						tileLabel.setText(target.getType().name());
					}
				}
			}
		}
		return tileButtonPanel;
	}
	/**
	 * Returns the selected tile label.
	 * @return The selected tile label.
	 */
	private JLabel getTileLabel() {
		if (tileLabel == null) {
			tileLabel = new JLabel("PASSABLE", JLabel.CENTER);
			tileLabel.setFont(new Font("San-Serif", Font.BOLD, 20));
		}
		return tileLabel;
	}
}
